//# DatabaseInterface.js
//# NGJurassic
//#
//# Created by Washaq.Majeed on 5/4/11.
//# Copyright 2011 __MyCompanyName__. All rights reserved.


exports.DatabaseInterface = Core.Class.singleton(
{
classname: 'DatabaseInterface',
	
initialize: function()
	{
		this._successCB = null;
		this._user = null;
		this._selectedMallId = null;
		this._isVisitingNeighbor = false;
	},
	
	startFetchingUserData: function(successCB)
	{
		this._successCB = successCB;
		//PersistanceManager.getUser(this.loadAppDataCallback.bind(this));
		this.startFetchingFromFile();
	},
	
	startFetchingFromFile: function()
	{
		var resources = ["User"];
		GAPersistenceManager.loadResources(resources, this.fetchingInProgress.bind(this), this.fetchingUserDone.bind(this));
	},
	
	loadAppDataCallback: function(error, data)
	{
		//console.log("Zulu: loadAppDataCallback");
		if(!error && data)
		{
			//console.log("Zulu: data "+data);
			//this._user = data;
			//console.log("Zulu:::: Yahan se User bana ha");
			this._user = JSON.parse(data);
			this._successCB();
		} 
		else
		{
			this.startFetchingFromFile();
		}
	},
	
	fetchingInProgress: function (loaded, total) {
		//console.log("Zulu: Progress: " + loaded + " / " + total);
	},
	
	fetchingUserDone: function () {
		//console.log("Zulu: User loaded!!!");
		this._user = GAPersistenceManager.getResource("User");
		//console.log("Zulu: User Json "+JSON.stringify(this._user));
		this._successCB();								   
	},
	
	getUserObj: function()
	{
		if(this._user) {
			return this._user;
		}
		return null;
	},
	
	loadNeighbor: function(path)
	{
        GameCentralHandler.destroyGameLayer();								 
		this.deleteUserState();

		this._filesToLoad = 1;
		this._filesLoaded = 0;
	    //Storage.FileSystem.readFile(path+DatabaseInterface.TR_USER_TABLE+'.json',this._dbFileReadCallback.bind(this,this.populateAllUsers.bind(this)));
	    var resource = "User";
	    this._user = new GAPersistedObject(resource,this.persistedObjectSuccessForNeighbor.bind(this),path);
	    
	    //Storage.FileSystem.readFile(path+'user'+'.json',this._dbFileReadCallback.bind(this,this.populateUser.bind(this)));
	},
	
	_dbFileReadCallback: function (func, error,contents)
	{//un used function so far
		if(!error){
			this._filesLoaded++;
			var data = eval('('+ contents +')');									 
			func(data);
			if(this._filesLoaded == this._filesToLoad)
			{	
				DatabaseHelper.loadUser();
			}
		}
	},
	
	persistedObjectSuccessForNeighbor: function()
	{
		//DatabaseHelper.loadUser();
		GameCentralHandler.loadSuccessForDatabaseInterface();
	},
	
	deleteNeighbourState: function()
	{
		this.deleteUserState();
	},
	
	loadUser: function()
	{
        GameCentralHandler.destroyGameLayer();
		this.deleteNeighbourState();
										 
	    var resources = ["User"];
	    GAPersistenceManager.loadResources(resources, this.callBackProgress.bind(this), this.doneUserLoading.bind(this));		
	},
	
	callBackProgress: function()
	{
		
	},
	
	doneUserLoading: function()
	{
		this.populateUser();

		GameCentralHandler.loadSuccessForDatabaseInterface();
	},
		
	populateUser: function(data)
	{
		if(data)
		{
			this._user = data;
		}
		else 
		{
			this._user = GAPersistenceManager.getResource("User");
		}
	},
	
	deleteUserState: function()
	{
		this.deleteUser();
	},
         
	deleteUser: function()
	{
		if(this._user)
		{
			this._user.destroy();
			delete this._user;
			this._user = null;
		}
	},
	
	updateUser:function() 
	{
		if(!this._isVisitingNeighbor)
		{	
			//var data = JSON.stringify(this._user);
			//PersistanceManager.updateUser(data);
			this._user.persist();
		}
	},
	
	getLevelInfo: function(){
		return this._gameLevels;
	},});